package com.example.demo.processor;

import com.example.demo.domain.ContentDO;
import com.example.demo.domain.MenuTagDO;
import com.example.demo.util.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.TreeSet;

@Component
public class MenuProcessor implements Processor {
    @Override
    public ContentDO process(String html) {
        if (StringUtil.isEmpty(html)) {
            return null;
        }
        Document doc = Jsoup.parse(html);
        Optional<Document> document = Optional.of(doc);
        TreeSet<MenuTagDO> menuTagDOS1 = document.map(document1 -> document1.selectFirst("#list>dl"))
                .map(dl -> {
                    Elements dts = dl.select("dt");
                    if (dts == null || dts.size() == 0) {
                        return null;
                    }
                    Element pointedElement = null;
                    for (int i = 0; i < dts.size(); i++) {
                        Element element = dts.get(i);
                        String text = element.text();
                        if (StringUtil.isEmpty(text)) {
                            continue;
                        }
                        if (text.contains("正文")) {
                            pointedElement = element;
                        }
                    }
                    if (pointedElement == null) {
                        return null;
                    }
                    TreeSet<MenuTagDO> menuTagDOS = new TreeSet<MenuTagDO>(
                            (m1, m2) -> m2.getId().compareTo(m1.getId())
                    );
                    int i = pointedElement.siblingIndex();
                    Elements elements = pointedElement.siblingElements();
//                    List<Element> elementList=new ArrayList<>(30);
                    for (Element element : elements) {
                        int siblingIndex = element.siblingIndex();
                        if (siblingIndex > i) {
//                            elementList.add(element);
                            Elements a = element.select("a");
                            if (a != null) {
                                MenuTagDO menuTagDO = new MenuTagDO();
                                String href = a.attr("href");
                                menuTagDO.setUrl(href);
                                menuTagDO.setText(a.text());
                                menuTagDO.setId(Integer.valueOf(href.substring(href.lastIndexOf("/") + 1, href.lastIndexOf("."))));
                                menuTagDOS.add(menuTagDO);
                            }
                        }
                    }
                    return menuTagDOS;
                }).get();
        if (menuTagDOS1 == null || menuTagDOS1.size() == 0) {
            return null;
        }
        StringBuilder tags = new StringBuilder();
        for (MenuTagDO menuTagDO : menuTagDOS1) {
            tags.append(menuTagDO.toString());
        }
        ContentDO contentDO = new ContentDO();
        contentDO.setShowContent(tags.toString());
        return contentDO;
    }
}
